<template>
  <div class="base-container" id="main">
    <t-form ref="form" :data="queryParams" label-width="80"
            @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}"
            @reset="()=>{this.queryParams.pageNum=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="标准名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入标准名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="标准状态" name="localStandardStatus ">
            <t-select clearable v-model="queryParams.localStandardStatus"
                      :options="dict.type.local_standard_status">
            </t-select>
          </t-form-item>
<!--          <t-form-item label="标准编号" name="projectNum">-->
<!--            <t-input-->
<!--              v-model="queryParams.projectNum"-->
<!--              clearable-->
<!--              placeholder="请输入标准编号"-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->
          <t-form-item label="起草单位" name="draftUnitNames">
            <t-input
              v-model="queryParams.draftUnitNames"
              clearable
              placeholder="请输入起草单位"
              style="width: 240px"
            />
          </t-form-item>
<!--          <t-form-item label="起草人员" name="draftPersonNames">-->
<!--            <t-input-->
<!--              v-model="queryParams.draftPersonNames"-->
<!--              clearable-->
<!--              placeholder="请输入起草单位"-->
<!--              style="width: 240px"-->
<!--            />-->
<!--          </t-form-item>-->
          <t-form-item label="申报日期" name="publicDateRange">
            <t-date-range-picker style="width: 280px" :placeholder="['开始时间','结束时间']"
                                 v-model="queryParams.publicDateRange" allow-input clearable/>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div>
      <t-table rowKey="index" size="small" :data="data" :columns="columns" hover :pagination="pagination"
               :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange">
        <template #projectName="{ row }">
          <t-link @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_detail=true" hover="color"
                  theme="primary">{{ row.projectName }}
          </t-link>
        </template>
        <template #reviseNum="{ row }">
          {{ row.reviseNum || "-" }}
        </template>
        <template #systemName="{ row }">
          {{ row.systemName || "-" }}
        </template>
        <template #consistencyDegree="{ row }">
          {{ row.consistencyDegree || "-" }}
        </template>
        <template #internationalStandardNum="{ row }">
          {{ row.internationalStandardNum || "-" }}
        </template>
        <template #formulateType="{ row }">
          {{ row.formulateType | filterByDict(dict.type.formulate_type) }}
        </template>
        <template #standardCategory="{ row }">
          {{ row.standardCategory | filterByDict(dict.type.standard_category) }}
        </template>
        <template #standardProperty="{ row }">
          {{ row.standardProperty | filterByDict(dict.type.standard_property) }}
        </template>
        <template #allOpinionCount="{ row }">
          <t-link @click="allOpinionCountClick(row,'yk')" hover="color" theme="primary">{{
              row.allOpinionCount
            }}
          </t-link>
        </template>
        <template #summaryOpinionCount="{ row }">
          <t-link @click="allOpinionCountClick(row,'yh')" hover="color" theme="primary">{{
              row.summaryOpinionCount
            }}
          </t-link>
        </template>
        <template #manageDeptId="{ row }">
          {{ row.manageDeptId | filterByTree(deptMenuOptions) }}
        </template>
        <template #localStandardStatus="{ row }">
          <t-tag @click="dialog.header=row.projectName;dialog.standardCategory=row.standardCategory;dialog.objId=row.id;dialog.open_approve_list=true" :theme="handleTheme(row.localStandardStatus, dict.type.local_standard_status)" variant="light">
            {{ row.localStandardStatus | filterByDict(dict.type.local_standard_status) }}
          </t-tag>
        </template>
        <template #op="{ row }">
          <div v-if="!['6','7'].includes(row.localStandardStatus)">
            <t-button size="small" @click="summary(row)" variant="text" class="t-button-link">
              <t-icon name="edit-1" slot="icon"/>
              汇总
            </t-button>
            <t-tooltip v-if="!['8'].includes(row.localStandardStatus)" content="汇总完成，提交至报审阶段">
              <t-button size="small" @click="summarizedBtn(row)" variant="text" class="t-button-link">
                <t-icon name="check" slot="icon"/>
                结束汇总
              </t-button>
            </t-tooltip>
          </div>
          <t-button
            v-if="['1','4','11', null, '8'].includes(row.localStandardStatus)"
            @click="update(row)"
            size="small"
            variant="text" class="t-button-link">
            <t-icon name="edit-1" slot="icon"/>
            继续申报
          </t-button>
          <div>
            <t-button size="small" @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_detail=true"
                      variant="text" class="t-button-link">
              <t-icon name="info-circle" slot="icon"/>
              详情
            </t-button>
          </div>
        </template>
      </t-table>
    </div>
    <standardDetailDialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail"
                          v-if="dialog.open_detail">
    </standardDetailDialog>
    <opinionDetailDialog @closeBzhDialog="closeBzhDialog" v-if="dialog.visible"
                         :dialogInfo="dialog"></opinionDetailDialog>
    <opinionInfo :info="opinionInfoDialog" @closeDialog="closeDialog" v-if="opinionInfoDialog.visible"></opinionInfo>
    <approveList :header="dialog.header" :standardCategory="dialog.standardCategory" :obj-id="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approveList>

    <t-dialog
      :header="nowRow.projectName"
      :visible.sync="summarizedShow"
      :closeOnOverlayClick="false"
      @confirm="summarized"
    >
      您是否完成意见汇总，确定提交至报审阶段！
    </t-dialog>
  </div>
</template>

<script>
import standardDetailDialog from "@/pages/bzh/standard/components/standard-detail-dialog.vue";
import opinionDetailDialog from "@/pages/bzh/opinion/components/opinion-detail-dialog.vue";
import opinionInfo from "@/pages/bzh/opinion/components/opinion-info.vue";
import approveList from "../approve/components/approve-list.vue";

export default {
  name: "opinin-my",
  dicts: ["formulate_type", "local_standard_status", "standard_category", "standard_property"],
  deptMenuOptions: [],
  components: {
    opinionInfo,
    opinionDetailDialog,
    standardDetailDialog,
    approveList
  },
  data() {
    return {
      summarizedShow: false,
      rules: {
        dealOpinion: [
          {
            required: true,
            message: "处理意见不能为空",
          },
        ],
        note: [
          {
            required: true,
            message: "备注不能为空",
          },
        ],
        summary: [
          {
            required: true,
            message: "汇总不能为空",
          },
        ],

      },
      formData: {
        dealOpinion: "",
        note: "",
        summary: "0"
      },
      nowRow: {},
      summaryShow: false,
      dialog: {
        objId: "",
        header: "",
        open_detail: false,
        standardCategory: '',
        open_approve_list: false,
      },
      opinionInfoDialog: {
        id: "",
        header: "",
        visible: false,
        // "yk"游客提出的意见列表 "yh"已汇总意见列表
        type: "yk"
      },
      dataLoading: false,
      sort: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: "",
        formulateType: "",
        publicDateRange: [],
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      data: [],
      columns: [
        {
          colKey: "projectName",
          title: "标准名称",
          align: "center",
          width: 160
        },
        {
          colKey: "allOpinionCount",
          title: "游客提出数量",
          align: "center",
          width: 140,
        },
        {
          colKey: "summaryOpinionCount",
          title: "已汇总数量",
          align: "center",
          width: 140,
        },
        {
          colKey: "systemName",
          title: "标准体系",
          align: "center",
          width: 160,
        },
        {
          colKey: "standardProperty",
          title: "标准性质",
          align: "center",
          width: 100,
        },
        {
          colKey: "standardCategory",
          title: "标准类别",
          align: "center",
          width: 100,
        },
        {
          colKey: "formulateType",
          title: "制定或修订",
          align: "center",
          width: 140,
        },

        {
          colKey: "manageDeptId",
          title: "主管部门",
          align: "center",
          width: 140,
        },
        {
          colKey: "completeDate",
          title: "完成时间",
          align: "center",
          width: 140,
          sorter: true,
        },

        {
          colKey: "reviseNum",
          title: "被修订标准号",
          align: "center",
          width: 140,
        },
        {
          colKey: "consistencyDegree",
          title: "一致性程度",
          align: "center",
          width: 140,
        },
        {
          colKey: "internationalStandardNum",
          title: "国际标准号",
          align: "center",
          width: 140,
        },
        {
          colKey: "localStandardStatus",
          title: "标准状态",
          align: "center",
          width: 180,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 160,
          fixed: 'right'
        },
      ]
    }
  },
  async created() {
    this.getDeptTreeSelect(this.$store.state.user.deptId)
    this.getList()
  },
  methods: {
    update(row){
      this.$router.push({
        path:`/bzh/other/apply/standard-apply/${row.id}`,
      });
    },
    summarizedBtn(row) {
      this.nowRow = row
      this.summarizedShow = true
    },
    summarized() {
      const tmpFormData = {
        id: this.nowRow.id,
        localStandardStatus: 6
      }
      this.$api.bzh.standard.updateStatus(tmpFormData).then(async res => {
        this.getList()
        this.$message.success("汇总完成")
        this.summarizedShow = false
      }).catch(err => {
        this.$message.error(err.toString())
      })
    },
    allOpinionCountClick(row, type) {
      this.opinionInfoDialog.type = type
      this.nowRow = row
      this.opinionInfoDialog.visible = true
      this.opinionInfoDialog.id = row.id
    },
    onSubmit({validateResult, firstError}) {
      if (validateResult === true) {
        this.formData.id = this.nowRow.id
        this.$api.bzh.opinion.summary(this.formData).then(response => {
          this.$message.success("汇总成功")
          this.summaryShow = false
        }).catch(e => {
          this.$message.error(e.toString())
        })
      } else {
        this.$message.error(firstError)
      }
    },
    // 汇总
    summary(row) {
      // this.opinionInfoDialog.visible = true
      if (row.localStandardStatus === "6") {
        this.$message.error("该标准已汇总完成")
        return
      }
      this.$router.push({
        path: "/bzh/opinion/opinion-summary",
        query: {id: row.id}
      })

    },
    // 关闭评论弹框
    closeDialog() {
      this.opinionInfoDialog.visible = false
    },
    // 关闭详情弹框
    closeBzhDialog(data) {
      this.dialog.visible = data
    },
    getDeptTreeSelect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    async getList() {
      try {
        [this.queryParams.startPublishDate, this.queryParams.endPublishDate] = this.queryParams.publicDateRange;
        this.dataLoading = true
        const response = await this.$api.bzh.opinion.myList(this.queryParams)
        this.data = response.rows
        this.pagination.total = response.total
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        await this.$message.error(e.toString())
      }

    },
    sortChange(sort) {
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
  }
}
</script>

<style lang="less" scoped>
.step-form {
  padding: 0 24px;
}

.t-row {
  margin-bottom: 14px;
}

.step-card {
  height: 100%;
  margin-bottom: 14px;
}
</style>

